package com.swordget.service.impl;

import com.swordget.entity.TableDesign;
import com.swordget.service.ImportDeleteService;
import com.swordget.service.SwitchingDataSourcesService;
import com.swordget.service.TableDefinitionService;
import com.swordget.service.TableManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Objects;

/***
 *  导入覆盖删除服务实现
 * @author feiyang.jia
 * @date 2024.5.13
 */
@Service
public class ImportDeleteServiceImpl implements ImportDeleteService {

    @Autowired
    private TableDefinitionService tableDefinitionService;



    @Autowired
    private SwitchingDataSourcesService switchingDataSourcesService;

    @Override
    public void deleteImport(String loginId, String tableId, String dataSourceId) {
        //获取表名
        String tableName = tableDefinitionService.getTableName(loginId, tableId);
        //根据表名删除数据
        String command = String.format("DELETE FROM %s", tableName);
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = switchingDataSourcesService.switchingDataSources(dataSourceId);
            ps = conn.prepareStatement(command);
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (ps != null) {
                    ps.close();
                }

                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
